/*
 * Copyright 2012-2020 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * @author lzhoumail@126.com/zhouli
 * Git http://git.oschina.net/zhou666/spring-cloud-7simple
 */
package com.nightsoul.spring.cloud.test.simple.ui.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.nightsoul.spring.cloud.test.simple.ui.entity.User;

@Service
public class UserService {
	//@Autowired
	 RestTemplate restTemplate = new RestTemplate();
	
	 /*@Autowired
	 FeignUserService feignUserService;*/
	 
	 final String SERVICE_NAME="cloud-simple-service";
	 
	 @Autowired
	 DiscoveryClient discoveryClient;
	 
	 @SuppressWarnings("unchecked")
	@HystrixCommand(fallbackMethod = "fallbackSearchAll")
	 public List<User> readUserInfo() {
		 System.out.println(discoveryClient.getServices());
		 System.out.println(discoveryClient.getInstances("cloud-simple-service").get(0).getHost());
		 System.out.println(discoveryClient.getInstances("cloud-simple-service").get(0).getPort());
		 System.out.println(discoveryClient.getInstances("cloud-simple-service").get(0).getUri());
		 
		 List<User> users = this.restTemplate.getForObject("http://127.0.0.1:8080/user", List.class);
		 
		 
		try {
			List<User> Object = restTemplate.getForObject("http://"+SERVICE_NAME+"/user", List.class);
		} catch (RestClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 System.out.println(users);
	     return users;
		 //return feignUserService.readUserInfo();
	     
	     
	 }
	 
	 
	 private List<User> fallbackSearchAll() {
		 System.out.println("HystrixCommand fallbackMethod handle!");
		 List<User> ls = new ArrayList<User>();
		 User user = new User();
		 user.setUsername("TestHystrixCommand");
		 ls.add(user);
		 return ls;
	 }
}
